/*
 * @版权所有(C): 1999-2021,西安交大捷普网络科技有限公司
 * @FilePath: \src\common\CommonUtils\StringUtils.cpp
 * @作者: 岳保保
 * @Date: 2023-01-04 16:30:16
 * @功能描述: string 字符串处理工具类
 */
#include "StringUtils.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <algorithm>
#include <math.h>
#include <iostream>
#include <memory>
#include <vector>

const std::string StringUtils::getStringInMid(const std::string &src, const std::string &head, const std::string &tile)
{
    auto headPost = src.find(head);
    auto tilePost = src.find(tile);
    return std::string(src, headPost + head.size(), tilePost - headPost - head.size());
}

std::string& StringUtils::replace(std::string& s, const std::string& from, const std::string& to)
{
    if (!from.empty())
    {
        for (size_t pos = 0; (pos = s.find(from, pos)) != std::string::npos; pos += to.size())
        {
            s.replace(pos, from.size(), to);
        }
    }
    return s;
}

int StringUtils::remove(std::string& str, std::string::value_type ch)
{
    int nCount = 0;
    for (auto ite = str.begin(); ite != str.end(); )
    {
        if (*ite == ch)
        {
            nCount++;
            ite = str.erase(ite);
        }
        else
        {
            ite++;
        }
    }
    return nCount;
}

void StringUtils::removeSpecialChar(std::string& str)
{
    std::string blanks("\f\v\r\t\n ");
    str.erase(0, str.find_first_not_of(blanks));
    str.erase(str.find_last_not_of(blanks) + 1);
}

std::string StringUtils::removeFrontSpace(std::string str)
{
    str.erase(0, str.find_first_not_of(" "));
    return str;
}

bool StringUtils::isSrcStringStartWith(const std::string& srcStr, const std::string& targetStr)
{
    // 如果源字符串小于目标字符串
    if (srcStr.size() < targetStr.size())
    {
        return false;
    }
    // 如果字符串内容相同
    if (srcStr == targetStr)
    {
        return true;
    }

    // 从源字符串中截取目标字符串同长度的字符串比较
    std::string tmpStr(srcStr, 0, targetStr.size());
    return (tmpStr == targetStr);
}


std::string StringUtils::trim(std::string& str, const std::string& pattern)
{
    if (str.empty())
    {
        return str;
    }
    str.erase(0, str.find_first_not_of(pattern));
    str.erase(str.find_last_not_of(pattern) + 1);
    return str;
}

bool StringUtils::matchStringNoCaseSensitive(const std::string& tagStr, const std::string& childStr)
{
    // 转为小写
    std::string tagLowerStr = tagStr;
    std::transform(tagLowerStr.begin(), tagLowerStr.end(), tagLowerStr.begin(), ::tolower);
    // 转为小写
    std::string childLowerStr = childStr;
    std::transform(childLowerStr.begin(), childLowerStr.end(), childLowerStr.begin(), ::tolower);

    // 不区分大小写匹配
    return (tagLowerStr.find(childLowerStr) != std::string::npos);
}

const std::string StringUtils::formString(const char* format, ...)
{
    // 格式化字符串
    va_list vArgs;
    va_start(vArgs, format);
    int size = 1 + vsnprintf(nullptr, 0, format, vArgs);
    va_end(vArgs);
    if (size <= 0)
    {
        return "";
    }
    size_t sz = static_cast<size_t>(size);
    std::unique_ptr<char[]> buf(new char[sz]);
    buf[sz - 1] = '\0';
    va_start(vArgs, format);
    vsnprintf(buf.get(), sz, format, vArgs);
    va_end(vArgs);
    return std::string(buf.get());
}
